#!/usr/bin/env bash
## Update: 2021-09-27

##快手
##滴滴果园
##欢太商城
##饿了么
##美团
##中国联通

##############################  主  要  代  码  ##############################
## 免责声明：当作者的脚本不可拉取时，会临时启用别人 FORK 或搬运的库代替

echo -e "更新日期：\033[33m2021-09-27\033[0m\n"

## 作者
author_list="ks_fruit didi_fruit HeytapTask ELM MT Chinaunicom"
author_name=(
  ks_fruit
  didi_fruit
  HeytapTask
  ELM
  MT
  Chinaunicom
)

## 定义下载代理 (非内置功能)
if [[ ${EnableExtraShellProxy} ]] && [[ ${EnableExtraShellProxy} == true ]]; then
  ProxyJudge=${GithubProxy}
else
  ProxyJudge=""
fi

## 快手
scripts_base_url_ks_fruit=https://raw.githubusercontent.com/passerby-b/ks_fruit/main/
my_scripts_list_ks_fruit="ks_fruit.js"

## 滴滴
scripts_base_url_didi_fruit=${ProxyJudge}https://raw.githubusercontent.com/passerby-b/didi_fruit/main/
my_scripts_list_didi_fruit="dd_bucketWater.js dd_fruit.js dd_fruit_code.js dd_cookie.js"

## 欢太
scripts_base_url_HeytapTask=${ProxyJudge}https://raw.githubusercontent.com/hwkxk/HeytapTask/main/
my_scripts_list_HeytapTask="notify.py main.py"

## 饿了么
## Public
scripts_base_url_ELM=https://gitee.com/xiecoll/radish-script/raw/master/ELM/
my_scripts_list_ELM="lb_elm.js"

## 美团
scripts_base_url_MT=https://gitee.com/xiecoll/radish-script/raw/master/MT/
my_scripts_list_MT="lb_meituan.js"

## 中国联通
scripts_base_url_Chinaunicom=https://gitee.com/xiecoll/radish-script/raw/master/
my_scripts_list_Chinaunicom="Chinaunicom.js"


##############################  随  机  函  数  ##############################
cd $ShellDir
git remote -v | grep "supermanito" -wq
[ $? -ne 0 ] && clear && echo -e "\033[31m\n非本项目用户禁止使用！\n\033[0m" && exit 1
rand() {
  min=$1
  max=$(($2 - $min + 1))
  num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
  echo $(($num % $max + $min))
}
index=0
for author in $author_list; do
  eval scripts_list=\$my_scripts_list_${author}
  eval url_list=\$scripts_base_url_${author}
  eval author="author_name[${index}]"

  ## 判断脚本来源仓库
  format_url=$(echo $url_list | awk -F '.com' '{print$NF}' | sed 's/.$//')
  if [[ $(echo $url_list | egrep -o "github|gitee") == "github" ]]; then
    repository_platform="https://github.com"
    repository_branch=$(echo $format_url | awk -F '/' '{print$4}')
    reformat_url=$(echo $format_url | sed "s|$repository_branch|tree/$repository_branch|g")
    [[ ${EnableExtraShellProxy} == true ]] && DownloadJudgment="(代理)" || DownloadJudgment=""
  elif [[ $(echo $url_list | egrep -o "github|gitee") == "gitee" ]]; then
    repository_platform="https://gitee.com"
    reformat_url=$(echo $format_url | sed "s|/raw/|/tree/|g")
    DownloadJudgment=""
  fi
  repository_url="$repository_platform$reformat_url"
  echo -e "[\033[33m更新\033[0m] ${!author} ${DownloadJudgment}"
  echo -e "[\033[33m仓库\033[0m] $repository_url"

  for js in $scripts_list; do
    croname=""
    script_cron_standard=""

    eval url=$url_list$js
    eval name=$js
    eval formatname=$(echo $js | awk -F '/' '{print$NF}')
    wget -q --no-check-certificate $url -O "$ScriptsDir/$name.new" -T 10
    if [ $? -eq 0 ]; then
      mv -f $ScriptsDir/$name.new $ScriptsDir/$name
      echo -e "[\033[32mDone\033[0m] $formatname"

      case $name in
      jddjCookie.js | sign_graphics_validate.js | JDJRValidator_Pure.js | function/*)
        continue
        ;;
      esac

      croname=$(echo "$name" | awk -F\. '{print $1}' | perl -pe "{s|^jd_||; s|^jx_||; s|^jr_||;}")
      script_cron_standard=$(cat $ScriptsDir/$name | grep "https" | awk '{if($1~/^[0-59]/) print $1,$2,$3,$4,$5}' | sort | uniq | head -n 1)
      case $name in
      jd_try_xh.js)
        script_cron="30 10 * * *" # 指定京东试用的定时
        ;;
      *)
        if [[ -z ${script_cron_standard} ]]; then
          tmp1=$(egrep "cron|script-path|tag|\* \*|$name" $ScriptsDir/$name | head -1 | perl -pe '{s|[a-zA-Z\"\.\=\:\:\_]||g;}')
          tmp2=$(echo "$tmp1" | awk -F '[0-9]' '{print$1}' | perl -pe '{s| ||g;}')
          script_cron=$(echo "$tmp1" | perl -pe "{s|${tmp2}||g;}" | awk '{if($1~/^[0-59]/) print $1,$2,$3,$4,$5; else if ($1~/^[*]/) print $2,$3,$4,$5,$6}')
        else
          script_cron=${script_cron_standard}
        fi
        ;;
      esac

      if [ -z "${script_cron}" ]; then
        cron_min=$(rand 1 59)
        cron_hour=$(rand 1 23)
        [ $(grep -c "$croname" $ListCrontabUser) -eq 0 ] && sed -i "/hang up/a${cron_min} ${cron_hour} * * * $TaskCmd $croname" $ListCrontabUser
      else
        [ $(grep -c "$croname" $ListCrontabUser) -eq 0 ] && sed -i "/hang up/a${script_cron} $TaskCmd $croname" $ListCrontabUser
      fi
    else
      [ -f $ScriptsDir/$name.new ] && rm -f $ScriptsDir/$name.new
      echo -e "[\033[31mERR!\033[0m] $formatname 更新失败"
    fi
  done
  echo ''
  index=$(($index + 1))
done
##############################  自  定  义  命  令  ##############################

## 删除不知如何产生的垃圾文件
DeleteCacheFiles=""
for del in ${DeleteCacheFiles}; do
  [ -f $ScriptsDir/$del ] && rm -rf $ScriptsDir/$del
done

## 安装模块
[ ! -d /usr/lib/node_modules/form-data ] && npm install -g form-data

## 删除脚本和定时
DeleteScripts="long_super_redrain.js long_half_redrain.js zy_qycl.js"
for del in ${DeleteScripts}; do
  [ -f $ScriptsDir/$del ] && rm -rf $ScriptsDir/$del && sed -i "/ $TaskCmd $(echo "$del" | awk -F\. '{print $1}' | perl -pe "{s|^jd_||; s|^jx_||; s|^jr_||;}")/d" $ListCrontabUser
done
